<?php
defined('SEJUJOPEFDNGJSL') or exit('Access denied');

if($_POST['submit']=='通过'){
	$fpn = fopen("convert.txt", "w+");
	if(flock($fpn,LOCK_EX | LOCK_NB))
    {
	if(empty($_POST["cbid"])) show_automsg('请选择要审核的单子',$GLOBALARR['curl'],1);
	$lid=$_POST["cbid"];
	$now_price=getprice();
	// $id=implode(",",$_POST["cbid"]);
	$tishi=0;
	// $json = $CoinClient->getinfo();
	// if (!isset($json['version']) || !$json['version']) {
	// 	show_automsg('钱包连接失败',$GLOBALARR['curl'],1);
	// }
	// if ($json['balance'] < 1) {
	// 		show_automsg('未获取到钱包的数量！',$GLOBALARR['curl'],1);
	// }
	
	foreach ($lid as  $v) {
	$id=$v;
	$db->query("select * from ld_transfer_coin where id=".$id,2);
	$transfer_coin=$db->rs;
	if($transfer_coin['coin']=='pbc'||empty($transfer_coin['coin'])){
		$CoinClient= $CoinClient;
	}elseif($transfer_coin['coin']=='btc'){
		$CoinClient= $CoinClient_btc;
	}if($transfer_coin['coin']=='ltc'){
		$CoinClient= $CoinClient_ltc;
	}
	$json = $CoinClient->getinfo();
	if (!isset($json['version']) || !$json['version']) {
		show_automsg('钱包连接失败',$GLOBALARR['curl'],1);
	}
	if ($json['balance'] < 1) {
			show_automsg('未获取到钱包的数量！',$GLOBALARR['curl'],1);
	}
	if($transfer_coin['ctype']==0){
		$sjnum=round($transfer_coin['ctotal']*(100-$GLOBALARR['txkbj'])/100/$now_price,2);
	}elseif($transfer_coin['ctype']==15||$transfer_coin['ctype']==16){
        $sjnum=round($transfer_coin['ctotal']*(100-$GLOBALARR['txkql'])/100,2);
	}else{
        $sjnum=round($transfer_coin['ctotal']*(100-$GLOBALARR['txkql'])/100/$now_price,2);
	}
	if(!empty($transfer_coin) && $transfer_coin['type']==0 && $transfer_coin['is_cross']==0){
		$num = abs($sjnum);
		$addr=$transfer_coin['caddress'];
		if (!regex($num, 'currency')) {
			show_automsg('兑换数量格式错误',$GLOBALARR['curl'],1);
		}

		$valid_res = $CoinClient->validateaddress($addr);
		if (!$valid_res['isvalid']) {
			show_automsg('不是一个有效的钱包地址',$GLOBALARR['curl'],1);
		}
		if ($valid_res['ismine']) {
			show_automsg('绑定的地址有误！',$GLOBALARR['curl'],1);
		}

		if ($json['balance'] < $num) {
			show_automsg('钱包余额不足',$GLOBALARR['curl'],1);
		}
		try{
			$db->begintransaction();

			$sql="update ld_transfer_coin set is_cross=1,sjnum=".$sjnum." where is_cross=0 and type=0 and id=".$transfer_coin['id'];
			$db->sitedbexec($sql);
			$sendrs = $CoinClient->sendtoaddress($addr, floatval($num));
			if($sendrs){
				$sqll="update ld_transfer_coin set txid='".$sendrs."' where  type=0 and id=".$transfer_coin['id'];
				$db->sitedbexec($sqll);
				record_log('审核成功'.$id);
			}else{
				throw new PDOException("钱包转账失败！!");
			}
			$db->committransaction();
		}catch(PDOException $e){
			$db->rollbacktransaction();
			continue;
		}
		
		$tishi=1;
	}else{
		show_automsg('记录已经审核过了',$GLOBALARR['curl'],1);
	}
  }
  if($tishi==1){
  	// var_dump($sendrs);
  	// var_dump($arr);
  	show_automsg('审核成功',$GLOBALARR['curl']);
  }else{
  	show_automsg('审核完成',$GLOBALARR['curl']);
  }
  flock($fpn,LOCK_UN);
}
fclose($fpn);
}
if($_POST['submit']=='不通过'){
	if(empty($_POST["cbid"])) show_automsg('请选择要审核的单子',$GLOBALARR['curl'],1);
	// $id=implode(",",$_POST["cbid"]);
	$lid=$_POST["cbid"];
	foreach ($lid as  $v) {
	$id=$v;
	try{
		$db->begintransaction();
		$db->query("select * from ld_transfer_coin where id=".$id,2);
	    $xx=$db->rs;
	    if($xx['is_cross']==2){
	    	continue;
	    }
	    $fxian=$xx['ctotal'];
	    if($xx['ctype']==0){
			//写进我的账户去本金
			writeaccount(array($xx['cuid'],"0",20,time(),41,$fxian,'审核失败返现共享积分'.$fxian));
		}elseif($xx['ctype']==1){
			writeaccount(array($xx['cuid'],"0",11,time(),42,$fxian,'审核失败返现利息'.$fxian));
		}elseif($xx['ctype']==2){
			writeaccount(array($xx['cuid'],"0",9,time(),43,$fxian,'审核失败返现领导奖'.$fxian));
		}elseif($xx['ctype']==3){
			writeaccount(array($xx['cuid'],"0",13,time(),44,$fxian,'审核失败返现晋级钱'.$fxian));
		}elseif($xx['ctype']==15){
			writeaccount(array($xx['cuid'],"0",156,time(),1561,$fxian,'审核失败返换BTC'.$fxian));
		}elseif($xx['ctype']==16){
			writeaccount(array($xx['cuid'],"0",157,time(),1571,$fxian,'审核失败返换LTC'.$fxian));
		} 
		$db->sitedbexec("update ld_transfer_coin set is_cross=2 where is_cross=0 and type=0 and id= ".$id);
		$db->committransaction();
	}catch(PDOException $e){
		$db->rollbacktransaction();
		a_bck("error");
	}
 }
	show_automsg('审核完成',$GLOBALARR['curl']);
}
$pagecount=getnums($_GET["pagecount"],10);
// $pagecount=10;
$option=getnums($_GET['option'],3);
$orderby=getnums($_GET['orderby'],2);
$quser=trim($_GET['quser']);

$tmp_arr['toption'].='<a href="'.getqueurl('option',1).'" class="'.geteqval(array($option,1,'taselbtn','thabtn')).'">今日新增</a>'
        . '<a href="'.getqueurl('option',2).'" class="'.geteqval(array($option,2,'taselbtn','thabtn')).'">全部</a>'
        . '<a href="'.getqueurl('option',3).'" class="'.geteqval(array($option,3,'taselbtn','thabtn')).'">未审核</a>'
        . '<a href="'.getqueurl('option',4).'" class="'.geteqval(array($option,4,'taselbtn','thabtn')).'">已审核</a>'
        . '<a href="'.getqueurl('option',5).'" class="'.geteqval(array($option,5,'taselbtn','thabtn')).'">审核未通过</a>'
        . '<a href="'.getqueurl('option',6).'" class="'.geteqval(array($option,6,'taselbtn','thabtn')).'">本金</a>'
        . '<a href="'.getqueurl('option',7).'" class="'.geteqval(array($option,7,'taselbtn','thabtn')).'">利息</a>'
        . '<a href="'.getqueurl('option',8).'" class="'.geteqval(array($option,8,'taselbtn','thabtn')).'">团队收益</a>'
        // . '<a href="'.getqueurl('option',9).'" class="'.geteqval(array($option,9,'taselbtn','thabtn')).'">晋级奖</a>'
        .'<br/><input type="submit" name="submit" onclick="checkone();"    value="通过" class="thabtn">'
        .'<input type="submit" name="submit" onclick="checkone();"    value="不通过" class="thabtn">'
        ;


$tmp_arr['torderby']='<a href="'.getqueurl('orderby',geteqval(array($orderby,1,'2','1'))).'" class="'.((strstr($orderby,'1')||strstr($orderby,'2'))?"tpaixusel":"tpaixu").'">'.geteqval(array($orderby,1,'时间↓','时间↑')).'</a>';

if(is_array($GLOBALARR['jjname'])){
	foreach ($GLOBALARR['jjname'] as $k => $v) {
		$tmp_arr['tcolor'].='<a href="javascript:;" class="theacolor levelcolor'.$k.'">'.$v.'</a>';
	}
}

$condition=' where type=0';

if($quser){
	if(strstr($quser,'-')){
		$qdate=explode('-',$quser);
		if($qdate[0]) $condition.=" and from_unixtime(ctime,'%Y')=".$qdate[0]."";
		if($qdate[1]) $condition.=" and from_unixtime(ctime,'%m')=".$qdate[1]."";
		if($qdate[2]) $condition.=" and from_unixtime(ctime,'%d')=".$qdate[2]."";
	}else{
		$condition.=" and cuname like '%".$quser."%'";
	}
}

if($option==1) $condition.=" and from_unixtime(ctime,'%Y-%m-%d')='".date('Y-m-d')."'";
if($option==3) $condition.=' and `is_cross`=0';
if($option==4) $condition.=' and is_cross=1';
if($option==5) $condition.=' and `is_cross`=2';
if($option==6) $condition.=' and `ctype`=0';
if($option==7) $condition.=' and `ctype`=1';
if($option==8) $condition.=' and `ctype`=2';
if($option==9) $condition.=' and `ctype`=3';

$order='';
if($orderby==1) $order=' order by ctime desc';
if($orderby==2) $order=' order by ctime asc';


$tmp_arr['tth']="<th><input type='checkbox' name='checkboxs' id='alls' value=''/></th><th>序号</th><th>会员名称</th><th>兑换类型</th><th>兑换数量</th><th>兑换单价</th><th>手续费</th><th>实际到账个数</th><th>总金额</th><th>电子钱包地址</th><th>兑换时间</th><th>状态</th>";


$psql="select count(0) from ld_transfer_coin $condition";
$db->query($psql,3);
$pcount=$db->fval;
$pagecount=$pagecount;
$astr="&lsaction=convert&pagecount=$pagecount&orderby=$orderby&option=$option&quser=$quser";
$pagearr=pagelist($pagecount,$pcount,$astr);
$offset=$pagearr[1];

$sql="select * from ld_transfer_coin $condition $order limit $offset,$pagecount";
$db->query($sql);
$ii=0;
foreach ($db->rs as $k => $v) {
	$ii++;
	$serialid=($pagearr[2]-1)*$pagecount+$ii;
	if($v['ctype']==0){
		$type="本金";
	}elseif($v['ctype']==1){
		$type="利息";
	}elseif($v['ctype']==2){
		$type="团队收益";
	}elseif($v['ctype']==3){
		$type="晋级奖";
	}elseif($v['ctype']==15){
		$type="BTC";
	}elseif($v['ctype']==16){
		$type="LTC";
	}
	if($v['is_cross']==0){
		$ztai="未审核";
	}elseif($v['is_cross']==1){
		$ztai="审核通过";
	}elseif($v['is_cross']==2){
		$ztai="审核不通过";
	}
	$tmp_arr['ttr'].='<tr class="levelcolor">
						<td><input type="checkbox" pro="pro"  name="cbid[]" value="'.$v['id'].'"></td>
						<td><span  title="'.$v["id"].'">'.$serialid.'</span></td>
                        <td>'.$v['cuname'].'</td>
						<td >'.$type.'</td>
						<td>'.$v['num'].'</td>
                        <td>'.$v['cprice'].'</td>
                        <td>'.$v['cfee'].'</td>
                         <td>'.$v['sjnum'].'</td>
                        <td>'.$v['ctotal'].'</td>
						<td>'.$v['caddress'].'</td>
						<td>'.date("Y-m-d H:i:s",$v['ctime']).'</td>
						<td>'.$ztai.'</td>';
	$tmp_arr['ttr'].='</tr>';
}


$tmp_arr['tcount']=$pcount;
$tmp_arr['ttitle']=getmenu($GLOBALARR['menu'],'convert');
$tmp_arr['tpage']=$pagearr[0];

function admin_huan($arr){
echo '	<h3>'.$arr['ttitle'].'</h3>
	<div class="main-r-body"><form method="get" action="?">
	<div class="main-r-item"><input type="submit" class="thabtn" value="查询"><input type="input" class="tsearch" name="quser"><span class="paixutxt">排序<span>：'.$arr['torderby'];
if(!empty($arr["twhereoption"])){
echo '<span class="colortxt">账户类型：<span>'.$arr["twhereoption"];
}
if(!empty($arr["tcolorone"])){
echo '<span class="colortxt">导出：<span>'.$arr['tcolorone'];
}
echo '</div>';
		$questr=explode("&",$_SERVER["QUERY_STRING"]);
		for($i=0;$i<count($questr);$i++){
			$questrl=explode("=",$questr[$i]);
			if(count($questrl)>0)
			{
				if($questrl[1]!=""){
					if(!($questrl[0]=="quser")){
						echo '<input type="hidden" name="'.$questrl[0].'" value="'.$questrl[1].'">';
					}
				}
			}
		}
echo '	</form><form method="post" action="'.geturl().'">
	<div class="main-r-item" '.$arr["styles"].'>'.$arr['toption'].'</div>	';

echo '<table class="thealist ttab">
		<tr>
'.$arr['tth'].'
		</tr>
		<tbody>';
if($arr['tcount']>0){
echo $arr['ttr'];
}
echo '	</tbody>
	</table></form>';
if($arr['tcount']==0){
echo '<div class="nodata">暂无数据</div>';
}else{
echo '	<div class="pages">
		<div class="floatr">
'.$arr['tpage'].'
		</div>
	</div>	';
}

echo '</div>';
}
admin_huan($tmp_arr);
?>
<style type="text/css">
	.main-r-item{height: auto!important;overflow: hidden;}
	.thabtn{margin-left: 5px;}
</style>
<script type="text/javascript">
    $("#alls").click(function(){
                   var aa = $(this).attr('checked');
                    if(!aa){
                      $('input[pro="pro"]').attr('checked', false);
                    }else{
                      $('input[pro="pro"]').attr('checked', true);
                    }
                  });
 </script>